package com.example.iccil.manager;


import cn.hutool.json.JSONUtil;
import com.example.iccil.common.ErrorCode;
import com.example.iccil.exception.BusinessException;
import com.example.iccil.model.dto.hunyuan.*;
import com.example.iccil.model.dto.picture.PicInfo;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.entity.StringEntity;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;


import java.io.File;

import java.util.ArrayList;
import java.util.List;

/**
 * @author yolo
 * @Program yolo-picture
 * @description 调用腾讯混元管理器
 * @create 2025-05-20 14:57
 **/
public class HunYuanManager {

    private static final String HunYuan_URI = "https://yuanqi.tencent.com/openapi/v1/agent/chat/completions";

    public static PicInfo getPictureInfo(String url){
        try (CloseableHttpClient httpClient = HttpClients.createDefault()){
            HttpPost request = new HttpPost(HunYuan_URI);
            request.setHeader("X-Source", "openapi");
            request.setHeader("Authorization", "Bearer c3FUET1SP71g220tMtpRddqbmRMWtTO3");
            request.setHeader("Content-Type", "application/json");
            // 构造请求体
            Content content = new Content();
            content.setType("text");
            content.setText(url);
            Message message = new Message();
            message.setRole("user");
            List<Content> contentList = new ArrayList<>();
            contentList.add(content);
            message.setContent(contentList);
            HunYuanPictureRequest hunYuanPictureRequest = new HunYuanPictureRequest();
            hunYuanPictureRequest.setStream(false);
            hunYuanPictureRequest.setUser_id("yolo");
            hunYuanPictureRequest.setAssistant_id("OZx9HMT4S7j6");
            List<Message> messageList = new ArrayList<>();
            messageList.add(message);
            hunYuanPictureRequest.setMessages(messageList);
            String jsonStr = JSONUtil.toJsonStr(hunYuanPictureRequest);
            request.setEntity(new StringEntity(jsonStr));
            try (CloseableHttpResponse response = httpClient.execute(request)){
                System.out.println("Response Code: " + response.getStatusLine().getStatusCode());
                String responseBody = EntityUtils.toString(response.getEntity());
                System.out.println("Response Body: " + responseBody);
                HunYuanResponse bean = JSONUtil.toBean(responseBody, HunYuanResponse.class);
                String responseContent = bean.getChoices().get(0).getMessage().getContent();
                PicInfo picInfo = JSONUtil.toBean(responseContent, PicInfo.class);
                System.out.println("picInfo = " + picInfo);
                return picInfo;
            }catch (Exception e){
                throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
            }
        } catch (Exception e) {
            throw new BusinessException(ErrorCode.SYSTEM_ERROR, e.getMessage());
        }
    }

}
